package com.yoyo.yyadsdk.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.yoyo.ad.contract.AdContract;
import com.yoyo.ad.main.IAdFactory;
import com.yoyo.ad.main.IAdInteractionListener;
import com.yoyo.ad.main.YoYoAd;
import com.yoyo.ad.main.YoYoAdManager;
import com.yoyo.yyadsdk.R;
import com.yoyo.yyadsdk.bean.dao.AdvScreenEntity;
import com.yoyo.yyadsdk.serv.WindowBaseService;
import com.yoyo.yyadsdk.util.AdvUtils;
import com.yoyo.yyadsdk.util.TimeUtils;

import static com.yoyo.yyadsdk.config.Constants.APP_INTERACTION;
import static com.yoyo.yyadsdk.config.Constants.INTERACTION;


/**
 * Created by Amn on 2019/4/20.
 */

public class KeepActivity extends AppCompatActivity {

    private static final String TAG = "KeepActivity";
    public static final String IS_APP = "isApp";
    private IAdFactory mAdFactory;
    private boolean isApp;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Window window = getWindow();
//        //设置activity 左上角
//        window.setGravity(Gravity.START | Gravity.TOP);
//        // 创建宽高都 是 1 像素
//        WindowManager.LayoutParams attributes = window.getAttributes();
//        attributes.width = 1;
//        attributes.height = 1;
//        attributes.x = 0;
//        attributes.y = 0;
//        window.setAttributes(attributes);
//        window.setFlags(0x80000000, 0x80000000);
        setContentView(new View(this));
        mContext = this;
        isApp = getIntent().getBooleanExtra(IS_APP, false);
        mAdFactory = YoYoAdManager.getAdFactory(this, new IAdInteractionListener() {
            @Override
            public void adReady(int requestCode, YoYoAd interactionAd) {
                if (mContext != null)
                    interactionAd.show((Activity) mContext);
            }

            @Override
            public void adShow(int requestCode) {
                AdvScreenEntity adv = getIntent().getParcelableExtra("adv");
                long duration = adv.getDuration();
                //记录本地数据
                AdvUtils.recordAdvExposed(adv.getId(), adv.getType());
                //延时关闭
                if (duration > 0) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            KeepActivity.this.finish();
                        }
                    }, duration * TimeUtils.SECOND);
                }
            }

            @Override
            public void adClick(int requestCode) {
                finish();
            }

            @Override
            public void adDismissed(int requestCode) {
                finish();
            }

            @Override
            public void adFail(int requestCode, String arg0) {
//                AdvScreenEntity adv = getIntent().getParcelableExtra("adv");
//                //记录本地数据
//                AdvUtils.recordAdvExposed(adv.getId(), adv.getType());
//                Log.e(TAG, arg0);
                finish();
            }
        });
        YoYoAdManager.getConfig(this, new AdContract.View() {
            @Override
            public void onAdSuccess() {
                if (mAdFactory != null)
                    mAdFactory.getInteraction(isApp ? APP_INTERACTION : INTERACTION, 1);
            }

            @Override
            public void onAdFail(String err) {
                Log.e(TAG, err);
            }
        });
        registerBroadcast();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        finish();
        /* --------禁用recent task ------- */
//        ActivityManager activityManager = (ActivityManager) getApplicationContext()
//                .getSystemService(Context.ACTIVITY_SERVICE);
//        activityManager.moveTaskToFront(getTaskId(), 0);
    }

//    @Override
//    public void onBackPressed() {
//        super.onBackPressed();
//    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_HOME) {
//            return true;
//        } else if (keyCode == KeyEvent.KEYCODE_BACK) {
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdFactory.destroy();
        unregisterReceiver(mDismissReceiver);
        mContext = null;
    }

    private DismissReceiver mDismissReceiver;

    private void registerBroadcast() {
        //注册接收异常广播消息
        mDismissReceiver = new DismissReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(com.yoyo.yyadsdk.broad.DismissReceiver.ACTION);
        registerReceiver(mDismissReceiver, filter);
    }


    public class DismissReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && com.yoyo.yyadsdk.broad.DismissReceiver.ACTION.equals(intent.getAction())) {
                finish();
            }
        }
    }
}